package br.ufcg.edu.algoritimo;

import br.ufcg.edu.util.Constantes;


public class DecodificadorHuffman {

	private final No raiz;
	private No noAtual;
	private int totalCaracteres;

	public DecodificadorHuffman(No raiz, int totalCaracteres) {
		this.raiz = raiz;
		this.totalCaracteres = totalCaracteres;
		noAtual = this.raiz;
	}
	
	public String decodificaCaracteres(char[] cs){
		StringBuilder st = new StringBuilder("");
		if(getTotalCaracteres() > 0){
			for (char c : cs) {
				
				
				if (c == Constantes.BIT_ZERO) {
					noAtual = noAtual.getEsquerda();
				} else {
					noAtual = noAtual.getDireita();
				}
				
				if (noAtual.isFolha()) {
					st.append(noAtual.getDescricao());
					totalCaracteres--;
					noAtual = this.raiz;
				}
							
			}
		}
		return st.toString();
	}

	public int getTotalCaracteres() {
		return totalCaracteres;
	}

	public void setTotalCaracteres(int totalCaracteres) {
		this.totalCaracteres = totalCaracteres;
	}
}
